//-----------------------------------------------------------------------
// <copyright file="a_BrandDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_BrandDAOBase
    {
        #region Common methods
        public virtual a_Brand Createa_BrandFromReader(IDataReader reader)
        {
            a_Brand item = new a_Brand();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("BrandID"))) item.BrandID = (int)reader["BrandID"];
                if (!reader.IsDBNull(reader.GetOrdinal("BrandNameVN"))) item.BrandNameVN = (string)reader["BrandNameVN"];
                if (!reader.IsDBNull(reader.GetOrdinal("BrandNameEN"))) item.BrandNameEN = (string)reader["BrandNameEN"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Brand methods
            
        public virtual void Createa_Brand(a_Brand a_Brand)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Brands_ins");
                
                database.AddInParameter(dbCommand, "@BrandNameVN", DbType.String, a_Brand.BrandNameVN);
                database.AddInParameter(dbCommand, "@BrandNameEN", DbType.AnsiString, a_Brand.BrandNameEN);
                database.AddOutParameter(dbCommand, "@BrandID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Brand.BrandID = (int)database.GetParameterValue(dbCommand, "@BrandID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Brand methods
        
        public virtual void Updatea_Brand(a_Brand a_Brand)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Brands_upd");            
                
                database.AddInParameter(dbCommand, "@BrandID", DbType.Int32, a_Brand.BrandID);
                database.AddInParameter(dbCommand, "@BrandNameVN", DbType.String, a_Brand.BrandNameVN);
                database.AddInParameter(dbCommand, "@BrandNameEN", DbType.AnsiString, a_Brand.BrandNameEN);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Brand methods
        public virtual void Deletea_Brand(int brandID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Brands_del");
                
                database.AddInParameter(dbCommand, "@BrandID", DbType.Int32, brandID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Brand methods
        
        public virtual a_Brand Geta_Brand(int brandID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Brands_sel");
                
                database.AddInParameter(dbCommand, "@BrandID", DbType.Int32, brandID);
                
                a_Brand a_Brand =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Brand = Createa_BrandFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Brand;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_BrandList methods
        public virtual a_BrandCollection Geta_BrandList(a_BrandColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Brands_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_BrandCollection a_BrandCollection = new a_BrandCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Brand a_Brand = Createa_BrandFromReader(reader);
                        a_BrandCollection.Add(a_Brand);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_BrandCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_BrandCollection Geta_BrandList(a_BrandColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_BrandList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

        #region Geta_CountryList_alphabet methods
        public virtual a_BrandCollection Geta_BrandList_alphabet(string mychar, a_BrandColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Brand_selList_alphabet");

                database.AddInParameter(dbCommand, "@Mychar", DbType.AnsiString, mychar);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_BrandCollection a_BrandCollection = new a_BrandCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Brand a_Brand = Createa_BrandFromReader(reader);
                        a_BrandCollection.Add(a_Brand);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_BrandCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_BrandCollection Geta_BrandList_alphabet(string mychar, a_BrandColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_BrandList_alphabet(mychar, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion
    }
}